package com.central.apps.controller;

import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.tls.MACAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.net.www.protocol.ftp.Handler;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/Outbbutton")
public class OutbbuttonController {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/Outbbutton")
    public String Outbbutton(@RequestBody Map<String,Object> params) throws Exception {
        String prnum = MapUtils.getString(params, "prnum");
        JSONObject json = new JSONObject();
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_pr where prnum='" + prnum + "' and type =1");
        if (CollectionUtils.isNotEmpty(bySql)){
            Map<String, Object> map = bySql.get(0);
            String id = MapUtils.getString(map, "id");//申请id
            String prdesc = MapUtils.getString(map, "prdesc");//描述
            String status = MapUtils.getString(map, "status");//状态
            String subjectno = MapUtils.getString(map, "subjectno");//课题号
            String proid = MapUtils.getString(map, "proid");//课题号
            String taskid = MapUtils.getString(map, "taskid");//任务编号
            String budgetitem = MapUtils.getString(map, "budgetitem");//预算项
            String applyby = MapUtils.getString(map, "applyby");//申请人id
            String deptcode = MapUtils.getString(map, "deptcode");
            String createuserdesc = MapUtils.getString(map, "createuserdesc");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_prline where prid ='" + id + "'");
            if (CollectionUtils.isNotEmpty(bySql1)){
                for (int i=0;i<bySql1.size();i++){
                    Map<String, Object> map1 = bySql1.get(i);
                    String id1 = MapUtils.getString(map1, "id");//采购申请行id
                    Float orderqty = MapUtils.getFloat(map1, "orderqty");//采购申请行数量
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select a.* from gq_prtdetailline a left join gq_purchasereceipt b on a.prtid=b.id where a.prlid ='" + id1 + "' and b.`STATUS`='已批准'\n");
                    if (CollectionUtils.isNotEmpty(bySql2)){
                        for (int j=0;j<bySql2.size();j++){
                            Map<String, Object> map2 = bySql2.get(j);
                            String itemnum = MapUtils.getString(map2, "itemnum");//接收行物料
                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select id from gq_item where itemnum='" + itemnum + "'");
                            String itemid="";
                            if(CollectionUtils.isNotEmpty(bySql3)){
                                itemid = MapUtils.getString(bySql3.get(0), "id");//物料id
                            }
                            String itemdesc = MapUtils.getString(map2, "itemdesc");//接收行物料名称
                            Float orderqty1 = MapUtils.getFloat(map2, "orderqty");//接收行采购数量
                            Float receiveqty = MapUtils.getFloat(map2, "receiveqty");//接收行入库数量
                            String warehousenum = MapUtils.getString(map2, "warehousenum");//接收行库房编码
                            String warehousedesc = MapUtils.getString(map2, "warehousedesc");//接收行库房名称
                            String binnum = MapUtils.getString(map2, "binnum");//货位
                            String batchnum = MapUtils.getString(map2, "batchnum");//批次
                            String orderunit = MapUtils.getString(map2, "orderunit");//计量单位
                            String model = MapUtils.getString(map2, "model");//型号
                            String specifications = MapUtils.getString(map2, "specifications");//规格
                            String unitcost = MapUtils.getString(map2, "unitcost");
                            String linecost = MapUtils.getString(map2, "linecost");
                            if (StringUtils.isEmpty(unitcost)){
                                unitcost="0";
                            }
                            if (StringUtils.isEmpty(linecost)){
                                linecost="0";
                            }
                            BigDecimal bigDecimal = new BigDecimal(unitcost);//单价
                            BigDecimal bigDecimal1 = new BigDecimal(linecost);//金额
                            //判断是否已经有该库房的出库申请
                            List<Map<String, Object>> bySql4 = handlerService.findBySql("select itemapplynum from gq_itemapply where Outbbutton=2 and prnum='" + prnum + "' and location='"+warehousenum+"' and status='新建'");
                            HashMap<String, Object> hashMap = new HashMap<>();
                            HashMap<String, Object> hashMap1 = new HashMap<>();
                            if (CollectionUtils.isNotEmpty(bySql4)){
                                Map<String, Object> map3 = bySql4.get(0);
                                String itemapplynum = MapUtils.getString(map3, "itemapplynum");
                                //添加子表
                                hashMap1.put("glnum",itemapplynum);
                                hashMap1.put("itemnum",itemnum);
                                hashMap1.put("itemid",itemid);
                                hashMap1.put("subjectno",subjectno);
                                hashMap1.put("proid",proid);
                                hashMap1.put("taskid",taskid);
                                hashMap1.put("itemEnity_description",itemdesc);
                                hashMap1.put("Orunit", orderunit);
                                hashMap1.put("quantity",orderqty);
                                hashMap1.put("itemEnity_model",model);
                                hashMap1.put("GRANTQTY",receiveqty);
                                hashMap1.put("collectime",new Date());
                                hashMap1.put("unitcost",bigDecimal);
                                hashMap1.put("linecost",bigDecimal1);
                                hashMap1.put("locations",warehousenum);
                                hashMap1.put("binnum",binnum);
                                hashMap1.put("lotnum",batchnum);
                                hashMap1.put("specifications",specifications);
                                handlerService.insert("gq_itemapplyline",hashMap1);
                            }else {
//                                String outonumNew="";//出库编号
//                                List<Map<String, Object>> bySql5 = handlerService.findBySql("SELECT itemapplynum from gq_itemapply where itemapplynum  LIKE '%CDKO%' order by itemapplynum desc limit 1");
//                                if(CollectionUtils.isNotEmpty(bySql5)){
//                                    Map<String, Object> map4 = bySql5.get(0);
//                                    String itemapplynum = MapUtils.getString(map4, "itemapplynum");
//                                    //int integer = Integer.valueOf(itemapplynum.substring(itemapplynum.length()-8));
//                                    int a =itemapplynum.indexOf("O");
//                                    String integer1=itemapplynum.substring(a+1,itemapplynum.length());
//                                    int integer=Integer.parseInt(integer1);
//                                    integer++;
//                                    outonumNew="CDKO"+integer;
//                                }else{
//                                    outonumNew="CDKO10000001";
//                                }
                                // 生成采购申请编码
                                Map<String, Object> poMap = new HashMap<>();
                                handlerService.addBizNum("itemapplyApp", "gq_itemapply", poMap);
                                // 查询采购申请编码
                                String outonumNew = (String) poMap.get("itemapplynum");

                                log.info("============"+outonumNew);
                                hashMap.put("itemapplynum",outonumNew);
                                hashMap.put("Outbbutton","2");//是否出库按钮生成
                                hashMap.put("prnum",prnum);
                                hashMap.put("description",prdesc);
                                hashMap.put("status","新建");
                                hashMap.put("location",warehousenum);
                                hashMap.put("locadesc",warehousedesc);
                                Date date = new Date();
                                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                hashMap.put("createtime",sdf.format(date));
                                hashMap.put("deptcode",deptcode);
                                hashMap.put("createuserdesc",createuserdesc);
                                hashMap.put("applicantid",applyby);
                                List<Map<String, Object>> bySql6 = handlerService.findBySql("select name from gq_person where id='" + applyby + "'");
                                String name = MapUtils.getString(bySql6.get(0), "name");
                                hashMap.put("applicant",name);
                                hashMap.put("budgetitem",budgetitem);
                                hashMap.put("issueType","领用出库");
                                hashMap.put("subjectno",subjectno);
                                hashMap.put("proid",proid);
                                hashMap.put("taskid",taskid);
                                handlerService.insert("gq_itemapply",hashMap);
                                //添加子表
                                hashMap1.put("glnum",outonumNew);
                                hashMap1.put("itemnum",itemnum);
                                hashMap1.put("itemid",itemid);
                                hashMap1.put("subjectno",subjectno);
                                hashMap1.put("proid",proid);
                                hashMap1.put("taskid",taskid);
                                hashMap1.put("itemEnity_description",itemdesc);
                                hashMap1.put("Orunit", orderunit);
                                hashMap1.put("quantity",orderqty);
                                hashMap1.put("itemEnity_model",model);
                                hashMap1.put("GRANTQTY",receiveqty);
                                hashMap1.put("collectime",new Date());
                                hashMap1.put("unitcost",bigDecimal);
                                hashMap1.put("linecost",bigDecimal1);
                                hashMap1.put("locations",warehousenum);
                                hashMap1.put("binnum",binnum);
                                hashMap1.put("lotnum",batchnum);
                                hashMap1.put("specifications",specifications);
                                handlerService.insert("gq_itemapplyline",hashMap1);
                            }
                        }
                        json.put("code", "0");
                        json.put("msg", "出库成功");
                    }else {
                        json.put("code", "1");
                        json.put("msg", "此物料没有入库，无法出库。");
                    }

                }
            }
        }
        return json.toString();
    }
}
